package com.controllers
{
	import com.GameModel;
	import com.datas.vo.ItemVO;
	import com.views.GameView;
	
	public class GamePlayLayerController
	{
		private var _view:GameView;
		
		public var firstFindVO:ItemVO = new ItemVO;
		public var secondFindVO:ItemVO = new ItemVO;
		
		private var routeList:Array = [];
		private var itemList:Array = [];
		
		private var gameModel:GameModel = GameModel.getInstance();
		public function GamePlayLayerController(p_view:GameView)
		{
			itemList = gameModel.itemList;
			_view = p_view;
		}
		
		public function itemSelect(p_vo:ItemVO):void{
			secondFindVO = firstFindVO;
			firstFindVO = p_vo;
			if (firstFindVO != null && secondFindVO != null && firstFindVO != secondFindVO) {
				if (firstFindVO.type == secondFindVO.type && findRoute(firstFindVO.row, firstFindVO.col, secondFindVO.row, secondFindVO.col)) {
					//得分  、得分达到停止生成随机
					gameModel.score+= 10
					if(gameModel.score>20){
						gameModel.isCreateRandom = false;
					}
					gameModel.itemList[firstFindVO.row][firstFindVO.col].id = 0;
					gameModel.itemList[secondFindVO.row][secondFindVO.col].id = 0;
					gameModel.itemList[firstFindVO.row][firstFindVO.col].type = 0;
					gameModel.itemList[secondFindVO.row][secondFindVO.col].type = 0;
					this._view.playLayerView.removeItem(this._view.playLayerView.getItemByVO(firstFindVO));
					this._view.playLayerView.removeItem(this._view.playLayerView.getItemByVO(secondFindVO));
					firstFindVO = secondFindVO = null;
				}
			}
		}
		private function findRoute(startX:int,startY:int,endX:int,endY:int ):Boolean {
			if (rowRoad1(startX,startY,endX,endY)||colRoad1(startX,startY,endX,endY)) {
				return true;
			}else {
				return false;
			}
		}
		private function rowRoad1(indexStart0:int,indexStart1:int,indexEnd0:int,indexEnd1:int):Boolean{
			for(var row:int = 0;row<itemList.length;row++){
				var routeX :int;
				var routeY : int;
				var tmpCount : int = 0;
				for(routeX = Math.min(indexStart1,indexEnd1);routeX<=Math.max(indexStart1,indexEnd1);routeX++){
					tmpCount += itemList[row][routeX].id;
				}
				for(routeY = Math.min(indexStart0,row);routeY<=Math.max(indexStart0,row);routeY++){
					if(routeY == row)continue;
					tmpCount+= itemList[routeY][indexStart1].id;
				}
				for(routeY = Math.min(indexEnd0,row);routeY<=Math.max(indexEnd0,row);routeY++){
					if(routeY == row)continue;
					tmpCount+= itemList[routeY][indexEnd1].id;
				}
				if(tmpCount == 2){
					routeList = new Array( );
					routeList.push([indexStart0, indexStart1], [row, indexStart1], [row, indexEnd1], [indexEnd0, indexEnd1]);
					return true;
				}
			}
			return false;
		}
		private function colRoad1(indexStart0:int,indexStart1:int,indexEnd0:int,indexEnd1:int):Boolean{
			for(var col:int = 0;col<itemList.length;col++){
				var routeX :int;
				var routeY : int;
				var tmpCount : int = 0;
				for( routeY = Math.min(indexStart0,indexEnd0);routeY<=Math.max(indexStart0,indexEnd0);routeY++){
					tmpCount+= itemList[routeY][col].id;
				}
				for(routeX = Math.min(col,indexStart1);routeX<=Math.max(col,indexStart1);routeX++){
					if(routeX == col)continue;
					tmpCount+= itemList[indexStart0][routeX].id;
				}
				for(routeX = Math.min(col,indexEnd1);routeX<=Math.max(col,indexEnd1);routeX++){
					if(routeX == col)continue;
					tmpCount+=itemList[indexEnd0][routeX].id
				}
				if(tmpCount == 2){
					routeList = new Array( );
					routeList.push([indexStart0, indexStart1], [indexStart0, col], [indexEnd0, col], [indexEnd0, indexEnd1]);
					return true;
				}
			}
			return false;
		}
		

	}
}